import request from "../utils/request";

/**
 * 0、获取所有的轮播位 /api/v1/banner/list
 * 1、获取轮播图、通过轮播位进行获取的
 * 2、定义相应的数据格式
 */

/**
 * {
 *     "statusCode": 200,
 *     "message": "获取成功",
 *     "data": {
 *         "info": {
 *             "_id": "6268bce29114094ca0ed2354",
 *             "isShow": true,
 *             "title": "首页顶部广告位",
 *             "description": "这个是首页顶部的广告位，主要是关于热门信息的简介",
 *             "banners": [
 *                 {
 *                     "_id": "627a8715362e459f8d65c091",
 *                     "title": "人民书记爱人民",
 *                     "type": 0,
 *                     "keyWord": "人民书记爱人民",
 *                     "description": "人民书记爱人民",
 *                     "bannerId": "6268bce29114094ca0ed2354",
 *                     "imageUrl": "https://yw.52kfw.cn/uploads/cover-1647057108397-875259682.jpg"
 *                 },
 *                 {
 *                     "_id": "627a8715362e459f8d65c092",
 *                     "title": "人民书记惦记大众",
 *                     "type": 0,
 *                     "keyWord": "人民书记惦记大众",
 *                     "description": "人民书记惦记大众",
 *                     "bannerId": "6268bce29114094ca0ed2354",
 *                     "imageUrl": "https://yw.52kfw.cn/uploads/cover-1647071908338-649655602.jpg"
 *                 },
 *                 {
 *                     "_id": "627a8715362e459f8d65c093",
 *                     "title": "人民书记和总理参加大会",
 *                     "type": 0,
 *                     "keyWord": "人民书记和总理参加大会",
 *                     "description": "人民书记和总理参加大会",
 *                     "bannerId": "6268bce29114094ca0ed2354",
 *                     "imageUrl": "https://yw.52kfw.cn/uploads/cover-1647057108397-875259682.jpg"
 *                 }
 *             ]
 *         }
 *     }
 * }
 */


interface IResponse{
    message: string;
    statusCode: number;
}

 interface IBannerResponse extends IResponse{
    data: {
        info:InfoItem;
    };
}
interface InfoItem {
    description: string;
    _id: string;
    title: string;
    banners: BannersItem[];
    isShow: boolean;
}
interface BannersItem {
    bannerId: string;
    imageUrl: string;
    description: string;
    _id: string;
    title: string;
    type: number;
    keyWord: string;
}
export const getBannersByPos = ():Promise<IBannerResponse>=>{
    let url='/api/v1/banner/bypos?id=6268bce29114094ca0ed2354'
    return request.get(url)
}
/**
 * 资讯类
 */

interface listItem {
    __v: number;
    _id: string;
    title: string;
    isShow: boolean;
}

interface INewsCategory  extends IResponse{
    data: {
        total: number;
        list: Array<listItem>;
    }
}
export const getNewsCategory = ():Promise<INewsCategory>=>{
    let url='/api/cate/flist'
    return request.get(url)
}
/**
 * 根据分类获取资讯列表
 */
interface INewParams{
    pageSize?:number;
    currentPage?:number;
    cate_id:string;
}


export interface NewslistItem {
    shortCut: string;
    createTime: string;
    author: string;
    imageUrl: string;
    __v: number;
    description: string;
    cate_id: string;
    _id: string;
    viewCount: number;
    title: string;
    isShow: boolean;
}

export interface INewsList extends IResponse{
    data: {
        total: number;
        list: Array<NewslistItem>;
    };
}

export const getNewListByCateId = ({pageSize=10,currentPage=1,cate_id}:INewParams):Promise<INewsList>=>{
    let url=`/api/news/flist?pageSize=${pageSize}&cate_id=${cate_id}&currentPage=${currentPage}`
    return request.get(url)
}

/**
 * 资讯详情接口
 */

interface infoItem {
    shortCut: string;
    createTime: string;
    author: string;
    imageUrl: string;
    __v: number;
    description: string;
    cate_id: string;
    _id: string;
    viewCount: number;
    title: string;
    isShow: boolean;
}

export interface INewDetail extends IResponse{
    data: {
        info:infoItem
    };
}


export const getNewsInfoById = (newsId:string):Promise<INewDetail>=>{
    let url=`/api/news/detail?newsId=${newsId}`
    return request.get(url)
}
/**
 * 资讯评论
 */

export const addCommentToNews = ({content,newsId,title="资讯评论"}):Promise<IResponse>=>{
    let url=`/api/comment/submitComment`
    return request.post(url,{content,newsId,title})
}

/**
 * 获取评论列表
 */

export interface commentListItem {
    userInfo: userInfoItem;
    questionId: string;
    createTime: string;
    __v: number;
    _id: string;
    userName: string;
    title: string;
    userId: string;
    content: string;
}

export interface userInfoItem {
    openid: string;
    imageUrl: string;
    name: string;
    _id: string;
    email: string;
}


export interface ICommentResponse {
    data: {
        commentList: Array<commentListItem>;
        total: number;
    };
    message: string;
    statusCode: number;
}

export const getCommentsByNewsId = ({newsId,pageSize=5,currentPage=1}:INewParams):Promise<ICommentResponse>=>{
    let url=`/api/comment/getCommentList?pageSize=${pageSize}&newsId=${newsId}&currentPage=${currentPage}`
    return request.get(url)
}

/**
 * 收藏列表
 */
export const getCollectNewsList  = (uid)=>{
    let url = '/api/questions/getCollectQuestion?pageSize=100&currentPage=1&userId=' + uid;
    return request.get(url)
}
/**
 * 取消收藏
 */
export const cancelCollect = (uid,id)=>{
    let url = '/api/questions/cancelCollectQuestion?userId=' + uid + '&questionId=' + id;
    return request.get(url)
}

/**
 * 收藏
 */

export const  collectNewsList= (uid,id) =>{
    let url = '/api/questions/collectQuestion?userId=' + uid + '&questionId=' + id;
    return request.get(url);
}
